package com.soft.javase.day08;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/4/27
 */
public class NumberStringSort {
    public static void main(String[] args) {
        String str = "9 10 12  6  8 15";

        String[] numStrArr = str.split("\\s+");
        System.out.println("按空格分割后的数字字符串数组："+Arrays.toString(numStrArr));

        List<Integer> numList = new ArrayList<>();
        for (String numStr : numStrArr) {
            if (!numStr.isEmpty()) {
                numList.add(Integer.valueOf(numStr));
            }
        }

        numList.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });

        String[] spaceStrArr = str.split("\\d+");
        System.out.println("按数字分割后的空格字符串数组："+Arrays.toString(spaceStrArr));

        StringBuilder result = new StringBuilder();
        for (int i = 0; i < numList.size(); i++) {
            if (i < spaceStrArr.length) {
                result.append(spaceStrArr[i]);
            }
            result.append(numList.get(i));
        }

        /*if (spaceStrArr.length > numList.size()) {
            result.append(spaceStrArr[spaceStrArr.length - 1]);
        }*/

        System.out.println("原始字符串: \"" + str + "\"");
        System.out.println("排序后的字符串: \"" + result + "\"");
    }
}
